package com.bkgy.jobs;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.bkgy.controller.acidefficiency.dao.AcidEfficiencyDao;
import com.bkgy.controller.acidefficiency.dao.TbTcm22280EffMapper;
import com.bkgy.controller.acidefficiency.entity.TbTcm22280Eff;
import com.bkgy.controller.acidefficiency.entity.TbTcm22280ProSpeed;
import com.bkgy.controller.tpdol3data.entity.TPdoDataL3Tcm2280;
import com.bkgy.controller.tpdol3data.service.ITPdoL3Tcm2280Service;
import com.bkgy.jobs.outputL3Listener.ITcm2280L3Listener;
import com.bkgy.controller.tbbxrecord.entity.TbBxFileSave;
import com.bkgy.controller.tbbxrecord.entity.TbBxSyncjob;
import com.bkgy.controller.tbbxrecord.service.ITbBxFileSaveService;
import com.bkgy.controller.tbbxrecord.service.ITbBxSyncService;
import com.bkgy.sys.common.constant.CommonConstant;
import com.bkgy.sys.common.dao.IConstMapper;
import com.bkgy.sys.common.entity.SysConst;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * 从宝信数据库同步秒用时信息
 */
@Slf4j
@Service
public class BxSyncSecJob implements ITcm2280L3Listener {
    @Autowired
    public ITbBxSyncService syncService;
    @Autowired
    public ITbBxFileSaveService fileSaveService;
    @Autowired
    public ITPdoL3Tcm2280Service tcm2280Service;
    @Autowired
    public AcidEfficiencyDao acidService;
    @Autowired
    public TbTcm22280EffMapper tcm22280EffMapper;
    @Autowired
    IConstMapper constMapper;

    public void exec() {
        int oldTopId = Integer.parseInt(CommonConstant.constMap.get(CommonConstant.ROLL_REC_ID));
        int topId = oldTopId;
        //根据最大值，查询新的同步记录
        List<TbBxSyncjob> bxSyncList = new ArrayList<>();
        List<TbBxFileSave> list = fileSaveService.loadNewBxFileSaveData(topId);
        //处理同步记录
        for (TbBxFileSave fileSave : list) {
            String line = fileSave.getLine();
            String message = fileSave.getMessage();
            JSONObject json = JSONObject.parseObject(message);
            String entId = json.getString("entId");
            String extId = json.getString("extId");

            if (StringUtils.isEmpty(line) || StringUtils.isEmpty(entId)) {
                continue;
            }

            TbBxSyncjob bxSyncjob = new TbBxSyncjob();
            bxSyncjob.setId(fileSave.getIndocno());
            bxSyncjob.setLine(line);
            bxSyncjob.setEntId(entId);
            bxSyncjob.setExtId(extId);

            //根据产线和入口卷号到宝信数据库查询计划秒时间、入轧机时间、生产结束时间
            List<TbBxSyncjob> dbDataList = syncService.findBxData(line, entId);
            if (dbDataList.size() > 0) {
                TbBxSyncjob dbData = dbDataList.get(0);
                bxSyncjob.setOutputSe(dbData.getOutputSe());
                Date tcTime = dbData.getTctime();
                Date endTime = dbData.getEndtime();
                bxSyncjob.setTctime(tcTime);
                bxSyncjob.setEndtime(endTime);
                if (tcTime != null && endTime != null) {
                    long l = (endTime.getTime() - tcTime.getTime()) / 1000;
                    bxSyncjob.setOutputSeActual((int)l);
                }
            }

            //根据出口卷号查询产量
            LambdaQueryWrapper<TPdoDataL3Tcm2280> w1 = new LambdaQueryWrapper<>();
            w1.eq(TPdoDataL3Tcm2280::getExtid, extId);
            TPdoDataL3Tcm2280 tcm2280 = tcm2280Service.getOne(w1);
            if (tcm2280 != null) {
                bxSyncjob.setOutMatThk(tcm2280.getOutMatThk());
                bxSyncjob.setOutMatWt(tcm2280.getOutMatWt());
                bxSyncjob.setOutMatWidth(tcm2280.getOutMatWidth());
                bxSyncjob.setShift(tcm2280.getShift());
                bxSyncjob.setCrew(tcm2280.getCrew());
                bxSyncjob.setStopFlag(0);//TODO
                bxSyncjob.setSlowFlag(0);//TODO
                bxSyncjob.setSteelgrade(tcm2280.getSteelgrade());
                bxSyncjob.setProdEndTime(tcm2280.getProdEndTime());
            }

            LambdaQueryWrapper<TbTcm22280ProSpeed> w2 = new LambdaQueryWrapper<>();
            w2.eq(TbTcm22280ProSpeed::getExtid, extId);
            TbTcm22280ProSpeed tbTcm22280ProSpeed = acidService.selectOne(w2);
            if (tbTcm22280ProSpeed != null) {
                bxSyncjob.setEnSpeedAvg(tbTcm22280ProSpeed.getEnSpeedAvg());
                bxSyncjob.setPickSpeed(tbTcm22280ProSpeed.getPickSpeed());
            }

            LambdaQueryWrapper<TbTcm22280Eff> w3 = new LambdaQueryWrapper<>();
            w3.eq(TbTcm22280Eff::getEntid, entId);
            TbTcm22280Eff tbTcm22280Eff = tcm22280EffMapper.selectOne(w3);
            if (tbTcm22280Eff != null) {
                bxSyncjob.setPorNo(tbTcm22280Eff.getPorNo());
                bxSyncjob.setEnSpeedSet(tbTcm22280Eff.getEnSpeedSet());
                bxSyncjob.setPlSpeedSet2(tbTcm22280Eff.getPlSpeedSet2());
            }

            bxSyncList.add(bxSyncjob);
            topId = fileSave.getIndocno();
        }
        syncService.saveOrUpdateBatch(bxSyncList);

        //更新最新记录编号
        if (topId > oldTopId) {
            constMapper.updateById(new SysConst(CommonConstant.ROLL_REC_ID, topId + "", null));
            CommonConstant.constMap.put(CommonConstant.ROLL_REC_ID, topId + "");
        }
    }

    //酸轧单卷生产效率分析功能，更新秒用时以外的数据
    @Override
    public void hasNew2280Record(List<TPdoDataL3Tcm2280> tcm2280List) {
        Map<String, Integer> map = new HashMap<>();
        List<String> extIdList = new ArrayList<>();
        for (TPdoDataL3Tcm2280 item : tcm2280List) {
            extIdList.add(item.getExtid());
        }
        //根据出口卷号获取id编号
        LambdaQueryWrapper<TbBxSyncjob> w1 = new LambdaQueryWrapper<>();
        w1.select(TbBxSyncjob::getExtId, TbBxSyncjob::getId);
        w1.in(TbBxSyncjob::getExtId, extIdList);
        List<TbBxSyncjob> list = syncService.list(w1);
        for (TbBxSyncjob item : list) {
            map.put(item.getExtId(), item.getId());
        }

        LambdaQueryWrapper<TbTcm22280ProSpeed> w2 = new LambdaQueryWrapper<>();
        w2.in(TbTcm22280ProSpeed::getExtid, extIdList);
        List<TbTcm22280ProSpeed> speedList = acidService.selectList(w2);
        Map<String, TbTcm22280ProSpeed> speedMap = new HashMap<>();
        for(TbTcm22280ProSpeed item : speedList) {
            speedMap.put(item.getExtid(),item);
        }

        List<TbBxSyncjob> objList = new ArrayList<>();
        for (TPdoDataL3Tcm2280 item : tcm2280List) {
            TbBxSyncjob obj = new TbBxSyncjob();
            obj.setId(map.get(item.getExtid()));
            obj.setSteelgrade(item.getSteelgrade());
            obj.setProdEndTime(item.getProdEndTime());
            obj.setOutMatWidth(item.getOutMatWidth());
            obj.setOutMatThk(item.getOutMatThk());
            obj.setOutMatWt(item.getOutMatWt());
            obj.setShift(item.getShift());
            obj.setCrew(item.getCrew());

            TbTcm22280ProSpeed speed = speedMap.get(item.getExtid());
            if(speed != null) {
                obj.setEnSpeedAvg(speed.getEnSpeedAvg());
                obj.setPickSpeed(speed.getPickSpeed());
            }

            objList.add(obj);
        }
        syncService.updateBatchById(objList);
    }
}
